/**
 * Copyright (c) Tiny Technologies, Inc. All rights reserved.
 * Licensed under the LGPL or a commercial license.
 * For LGPL see License.txt in the project root for license information.
 * For commercial licenses see https://www.tiny.cloud/
 *
 * Version: 5.8.2 (2021-06-23)
 */
(function () {
  "use strict";
  tinymce.PluginManager.add("addQues", function (editor) {
    var maxNum = editor.getParam('maxNum') || 20;
    function changeSort(editor) {
      setTimeout(function() {
        var quesDom = editor.$(".question");
        for (var i = 0; quesDom && i < quesDom.length; i++) {
          quesDom[i].innerHTML = '[' + (i + 1) + ']';
        }
      });
    }
    // 注册一个工具栏按钮名称
    editor.ui.registry.addButton("addQues", {
      text: "添加小题",
      tooltip: "添加小题",
      onAction: function () {
        if (editor.$(".question") && editor.$(".question").length < parseInt(maxNum)) {
          editor.insertContent(
            '&nbsp;<span class="question" contenteditable="false">&nbsp;</span>&nbsp;'
          );
        }
        changeSort(editor);
        editor.on("keyup", function() {
          changeSort(editor)
        });
        editor.on("change", function() {
          changeSort(editor)
        });
      },
    });
  });
})();